import modelEnhance from '@/utils/modelEnhance';
import PageHelper from '@/utils/pageHelper';
/**
 * 当第一次加载完页面时为true
 * 可以用这个值阻止切换页面时
 * 多次初始化数据
 */
let LOADED = false;
export default modelEnhance({
  namespace: 'lessons',

  state: {
    pageData: PageHelper.create(),
    lecturers: [],
    lectureType: []
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/lessons' && !LOADED) {
          // LOADED = true;
          dispatch({
            type: 'init'
          });
        }
      });
    }
  },

  effects: {
    // 进入页面加载
    *init({ payload }, { call, put, select }) {
      const { pageData } = yield select(state => state.lessons);
      yield put({
        type: 'getPageInfo',
        payload: {
          pageData: pageData.startPage(1, 10)
        }
      });
      yield put({
        type: 'getLecturers'
      });
      yield put({
        type: 'getLectureType'
      })
    },
    // 获取分页数据
    *getPageInfo({ payload }, { call, put }) {
      const { pageData } = payload;
      yield put.resolve({
        type: '@request',
        afterResponse: resp => resp.data,
        payload: {
          url: '/api/lesson/lessonInfoAllList',
          data: pageData.pageinfoData(),
          actionType: 'setLessonList',
        }
      })
    },
    // 保存 之后查询分页
    *save({ payload }, { call, put, select, take }) {
      const { values, success } = payload;
      // change the form value
      values.cover = values.cover[0].response.data.fileName;
      values.lecturerIds = values.lecturerList.length >0 ? values.lecturerList.join(',') : '';
      const { pageData } = yield select(state => state.lessons);
      // put是非阻塞的 put.resolve是阻塞型的
      yield put.resolve({
        type: '@request',
        payload: {
          notice: true,
          url: '/api/lesson/addLessonInfo',
          data: values
        }
      });
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();
    },
    // 修改
    *update({ payload }, { call, put, select }) {
      const { values, success } = payload;

      if (values.cover[0].response) values.cover = values.cover[0].response.data.fileName;else values.cover = null;
      values.lecturerIds = values.lecturerList.length >0 ? values.lecturerList.join(',') : '';

      const { pageData } = yield select(state => state.lessons);
      yield put.resolve({
        type: '@request',
        payload: {
          notice: true,
          url: '/api/lesson/updateLessonInfo',
          data: values
        }
      });

      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();
    },
    // 删除 之后查询分页
    *remove({ payload }, { call, put, select, all }) {
      const { records, success } = payload;
      const { pageData } = yield select(state => state.lessons);

      yield all(records.map(item => put({
        type: '@request',
        payload: {
          notice: true,
          method:'DELETE',
          url: '/api/lesson/deleteLessonInfo/' + item.id,
        }
      })));
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();
    },
    // 获取主讲老师列表
    *getLecturers({ payload }, { call, put }) {
      yield put.resolve({
        type: '@request',
        afterResponse: resp => resp.data.records.map(item=> {return {'key': item.id, 'title': item.realname}}),
        payload: {
          valueField: 'lecturers',
          url: '/api/teacher/allList',
          data: {},
        }
      });
    },
    *getLectureType({payload}, { call, put}){
      yield put.resolve({
        type: '@request',
        afterResponse: resp=> resp.data.map(item => {return {id: item.id, name: item.categoryName, child: item.categoryList}}),
        payload: {
          valueField: 'lectureType',
          method: 'GET',
          url: '/api/lesson/categoryList'
        }
      })
    },
    *lessonPublish({payload}, {put, select}){
      const {record, val} = payload;
      const { pageData } = yield select(state => state.lessons);

      yield put.resolve({
        type: '@request',
        payload:{
          url: '/api/lesson/isPublicLessonInfo',
          data: {id: record.id, isPublic: val},
          notice: true,
        }
      });
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
    },

  },

  reducers: {
    setLessonList_SUCCESS(state, {payload}){
      const {pageData} = state;
      return {
        ...state,
        pageData: pageData.setPage({data: payload})
      }
    }
  }
});
